package com.kim.bookshop.category.action;


import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;

import cn.itcast.commons.CommonUtils;

import com.kim.bookshop.category.bean.Category;
import com.kim.bookshop.category.service.CategoryService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class CateroryAction extends ActionSupport implements ModelDriven<Category>{
	
	private static final long serialVersionUID = -1271058862190128994L;
	private HttpServletRequest request;
	private HttpSession session;
	private ServletContext application;
	private ApplicationContext ctx;	
	private Category category=new Category();
	private CategoryService categoryService;
	
	public CateroryAction(){
		request=ServletActionContext.getRequest();
		session=request.getSession();
		application= session.getServletContext();
		ctx=(ApplicationContext) application.getAttribute("ApplicationContext"); 
		categoryService=(CategoryService) ctx.getBean("categoryService");
	}
	
	/**
	 * 所有分类
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		request.setAttribute("categoryList", categoryService.findAll());
		return "list";
	}
	
	/**
	 * 新增分类
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {
		category.setCid(CommonUtils.uuid());
		categoryService.add(category);
		return "add";
	}
	
	/**
	 * 修改分类
	 * @return
	 * @throws Exception
	 */
	public String mod() throws Exception {
		String cid=category.getCid();
		Category c=categoryService.load(cid);
		request.setAttribute("category", c);
		return SUCCESS;
	}
	
	/**
	 * 编辑分类
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {
		categoryService.edit(category);
		return "add";
	}
	
	/**
	 * 删除分类
	 * @return
	 * @throws Exception
	 */
	public String delete()  {
		String cid=category.getCid();
		System.out.println(cid);
		categoryService.remove(category.getCid());
		return "add";
	}
	
	//导出分类到excel

	
	@Override
	public Category getModel() {
		return category;
	}
}
